﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Zoomtel.Entity;
using Zoomtel.Persist.BIZ;
using Zoomtel.Service.BIZ.ViewModels;
using Zoomtel.Utils.Helpers;

namespace Zoomtel.Service.BIZ
{
   public class BankInfoService
    {
        BankInfoRepository bankInfoRepository;
        ILoginInfo loginInfo;
        public BankInfoService(BankInfoRepository _bankInfoRepository,ILoginInfo _loginInfo)
        {
            bankInfoRepository = _bankInfoRepository;
            loginInfo = _loginInfo;
        }
        public async Task<IResultModel> GetBankInfo()
        {
            Zoomtel.Entity.BIZ.BankInfoEntity entity = bankInfoRepository.FindEntity(a => a.AgentId == loginInfo.Uid);
            if (entity == null)
            {
                return ResultModel.Failed();
            }
            return ResultModel.Success(entity);
        }


        public async Task<IResultModel> SaveBankInfo(BankInfoAddModel model)
        {

            if (model.Type != 0 && model.Type != 1 && model.Type != 2)
            {
                return ResultModel.Failed("无效的收款方式");
            }
            if (model.PersonName.IsNull())
            {
                return ResultModel.Failed("收款人姓名不能为空");
            }
            if (model.BankCard.IsNull())
            {
                return ResultModel.Failed("收款账号不能为空");
            }
            if (model.Type == 1 || model.Type == 2)
            {
                if (model.QrcodeData.IsNull())
                {
                    return ResultModel.Failed("收款二维码不能为空");
                    
                }

            }
            if (model.Type == 0)
            {
                if (model.BankId == "")
                {
                    return ResultModel.Failed("开户银行错误！");
                }
                if (string.IsNullOrEmpty(model.BankAddress))
                {
                    return ResultModel.Failed("开户银行详细地址信息不能为空！");
                }

                if (!StringHelper.RegCheck(model.BankCard, "^([1-9]{1})(\\d{14}|\\d{18})$"))
                {

                    return ResultModel.Failed("开户银行卡号错误！");

                }

            }
            else
            {
                model.BankId = "";
                model.BankAddress = "";
            }


            Zoomtel.Entity.BIZ.BankInfoEntity entity = bankInfoRepository.FindEntity(a => a.AgentId == loginInfo.Uid);
            string action = "insert";
            if (entity == null)
            {
                action = "insert";
                entity = new Entity.BIZ.BankInfoEntity();

            }
            else
            {
                action = "edit";
                if (entity.Status == "10")
                {
                    return ResultModel.Failed("收款信息已审核无法进行修改");

                }
            }

            entity.UpdateBy = "";
            entity.UpdateTime = DateTime.Now;
            entity.AgentId = loginInfo.Uid;
            entity.BankCard = model.BankCard;
            entity.BarPath = model.QrcodeData;
            entity.PersonName = model.PersonName;
            entity.Type = model.Type;
            entity.Status = "0";

            int result = 0;

            if (action == "insert")
            {
                entity.Id = bankInfoRepository.GetSeq("S_BIZ_BANKINFO").ToString();
                result = await bankInfoRepository.InsertAsync(entity);

            }
            else
            {
                result = await bankInfoRepository.UpdateAsync(entity);
            }
            if (result > 0)
            {
                return ResultModel.Success();

            }
            else
            {
                return ResultModel.Failed();
            }
        }
    }
}
